package com.hulk.dryad.common.constant.enums;

import com.google.common.base.Objects;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.EnumSet;


/**
 * @author hulk
 */

public enum BEC {

    S0(0, "S_0"),
    E1(1, "E_1"),

    E_1000(1000, "E_1000"),
    E_1001(1001, "E_1001"),
    E_1002(1002, "E_1002"),
    E_1003(1003, "E_1003"),
    E_1004(1004, "E_1004"),
    E_1005(1005, "E_1005"),

    E_2000(2000, "E_2000"),
    E_2001(2001, "E_2001"),
    E_2002(2002, "E_2002"),
    E_2003(2003, "E_2003"),
    E_2004(2004, "E_2004"),
    E_2005(2005, "E_2005"),
    E_2006(2006, "E_2006"),
    E_2007(2007, "E_2007"),
    E_2008(2008, "E_2008"),
    E_2009(2009, "E_2009"),
    E_2010(2010, "E_2010"),
    E_2011(2011, "E_2011"),
    E_2012(2012, "E_2012"),
	E_2013(2013, "E_2013"),
	E_2014(2014, "E_2014"),
	E_2015(2015, "E_2015"),
	E_2016(2016, "E_2016"),
	E_2017(2017, "E_2017"),
	E_2018(2018, "E_2018"),
	E_2019(2019, "E_2019"),
	E_2020(2020, "E_2020"),
	E_2021(2021, "E_2021"),
	E_2022(2022, "E_2022"),
	E_2023(2023, "E_2023"),



    E_9999(9999, "E_9999");


	/**
	 * errCode  错误编码
	 */
    private int errCode;

	/**
	 * errDesc 错误描述
	 */
    private String errDesc;

    BEC(int errCode, String errDesc) {
        this.errCode = errCode;
        this.errDesc = errDesc;
    }


    public int getErrCode() {
        return errCode;
    }

    /**
     * @return the errDesc，根据语言环境返回国际化字符串
     */
    public String getErrDesc() {
        return messageSource.getMessage(errDesc, null, errDesc, LocaleContextHolder.getLocale());
    }

    public String getErrDesc(String [] args) {
        return messageSource.getMessage(errDesc, args, errDesc, LocaleContextHolder.getLocale());
    }

	@Override
	public String toString() {
		return "BEC{" +
				"errCode=" + getErrCode() +
				", errDesc='" + getErrDesc() + '\'' +
				'}';
	}

	private MessageSource messageSource;

    public BEC setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
        return this;
    }


    @Component
    public static class ReportTypeServiceInjector {

        @Autowired
        private MessageSource messageSource;

        @PostConstruct
        public void postConstruct() {
            for (BEC ec : EnumSet.allOf(BEC.class)) {
                ec.setMessageSource(messageSource);
            }
        }
    }

    public static String explain(String errcode) {
        for (BEC bussErrorCode : values()) {
            if (Objects.equal(errcode, bussErrorCode.errCode)) {
                return bussErrorCode.errDesc;
            }
        }
        return errcode;
    }

    public static BEC match(String respCode) {
        for (BEC bussErrorCode : values()) {
            if (!Strings.isNullOrEmpty(respCode)) {
                if (Objects.equal(Integer.valueOf(respCode), bussErrorCode.errCode)) {
                    return bussErrorCode;
                }
            }
        }
        return BEC.E_9999;
    }


}
